package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.ui.holder.PicHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by apple on 2017/4/27.
 */

public class PicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //两个final int类型表示ViewType的两种类型
    private final int NORMAL_TYPE = 0;
    private final int FOOT_TYPE = 1111;


    private List<String> picList;
    private Context context;

    private OnItemClickListener mOnItemClickListener;


    public PicAdapter(Context context, ArrayList<String> picList) {
        this.context = context;
        this.picList = picList;
    }

    public void setPicList(List<String> picList) {
        this.picList = picList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == picList.size()) {
            return FOOT_TYPE;
        }
        return NORMAL_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PicHolder picHolder = new PicHolder(View.inflate(context, R.layout.item_invite_pic, null));
        FootHolder footHolder = new FootHolder(View.inflate(context, R.layout.item_invite_pic_foot, null));
        if (viewType == FOOT_TYPE) {
            return footHolder;
        }
        return picHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == NORMAL_TYPE) {
            ((PicHolder) holder).setData(picList, position);
        }
        if (mOnItemClickListener != null) {
            holder.itemView.findViewById(R.id.ib_delete).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onDelClick(position);
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (picList == null) {
            return 1;
        }
        if (picList.size() < 3) {
            return picList.size() + 1;
        } else {
            return picList.size();
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onDelClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    static class FootHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_pic)
        ImageView ivPic;
        @BindView(R.id.ib_delete)
        ImageButton ibDelete;

        public FootHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ibDelete.setVisibility(View.GONE);
        }
    }
}
